// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-15

package remove

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/logics/master/setting/seat"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/kind"
	"server/app/lib"
)

// Logic
// 删除钓位.
type Logic struct {
	seat.Logic

	admin  *models.ArcadeAdmins
	arcade *models.Arcades

	affects     int64
	kind        kind.ArcadeAdminOperation
	kindContent string
	pond        *models.ArcadePond
	seat        *models.ArcadePondSeat

	request  *Request
	response *Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		request:  &Request{},
		response: &Response{},
	}
}

// After
// 后置日志.
func (o *Logic) After(ctx context.Context, _ iris.Context, _ result.Result) {
	if o.affects > 0 {
		_, _ = services.NewArcadeLogsService().Add(ctx,
			o.arcade, o.admin, o.kind, o.kindContent,
		)
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Json(i, o.request); err == nil {
		o.arcade, o.admin = lib.Arcade.GetMaster().GetArcade(i)
	}
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err error
		res = result.New()
	)
	if err = db.Transaction(ctx,
		o.RunCheck,
		o.RunRemove,
		o.RunStatistic,
	); err != nil {
		return res.WithError(err)
	}

	o.response.Id = o.seat.Id
	o.response.Name = o.seat.Name
	return res.With(o.response)
}

// RunCheck
// 查询数据.
func (o *Logic) RunCheck(ctx context.Context, sess *db.Session) (err error) {
	var has bool

	// 1. 查询鱼塘.
	if o.pond, has, err = services.NewArcadePondService(sess).Dao.GetById(ctx, o.request.PondId); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 无效鱼塘.
	if !has || o.pond.ArcadeId != o.arcade.Id || o.pond.Deleted.IsYes() {
		err = errs.ErrArcadePondNotFound
		return
	}

	// 3. 查询钓位.
	if o.seat, has, err = services.NewArcadePondSeatService(sess).Dao.GetById(ctx, o.request.Id); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 4. 无效钓位.
	if !has || o.seat.PondId != o.pond.Id || o.seat.Deleted.IsYes() {
		err = errs.ErrArcadeSeatNotFound
	}
	return
}

// RunRemove
// 执行软删.
func (o *Logic) RunRemove(ctx context.Context, sess *db.Session) (err error) {
	if o.affects, err = services.NewArcadePondSeatService(sess).Dao.UpdateFieldsById(ctx, map[string]any{
		"deleted":    kind.DeletedYes,
		"deleted_at": db.Now(),
	}, o.seat.Id); err != nil {
		err = errs.ErrDatabaseQuery
	} else if o.affects > 0 {
		o.kind = kind.ArcadeAdminOperationSeatRemove
		o.kindContent = o.kind.ToSeatRemove(o.pond.Name, o.seat.Name)
	}
	return
}

// RunStatistic
// 重建统计.
func (o *Logic) RunStatistic(_ context.Context, sess *db.Session) (err error) {
	if o.response.SeatTotal, err = o.CalcTotal(sess, o.pond.Id); err == nil {
		if o.response.SeatCount, err = o.CalcEnables(sess, o.pond.Id); err == nil {
			err = o.CalcUpdate(sess, o.pond.Id, o.response.SeatTotal, o.response.SeatCount)
		}
	}
	return
}
